thin.imports('/public/home/css/global.project.css');
thin.imports('/public/configure/conf.advisory.js');

thin.use(function() {
	// 路由
	checkLocation();

	$('#advisory-template').on('click', '#btn-des', function() {
		imid = $(this).attr('data-id');
		checkLocation('details');
	});
});

var imid,
	data_imdetail = {},
	__getdata_imdetail = {},
	jude_im = false;

// 获取资讯回调
function __complete_imvolist(res) {
	$.hideLoading();
	if(res.status == 200) {
		if(res.data.list.length > 0) {
			createlist(res.data.list);
		} else {
			$('#advisory-template').parent().addClass('nothing-bg');
		}
	} else {
		// noting
		$('#advisory-template').parent().addClass('nothing-bg');
	}
}

// 创建列表
function createlist(data) {
	var ele = '';
	for(var i in data) {
		var list = data[i];
		ele += '<a href="javascript:void(0);" class="weui-panel weui-panel_access bg-ffffff m-t-1x" style="display: block;" id="btn-des" data-id="' + list.id + '">';
		ele += '<div class="weui-media-box weui-media-box_appmsg weui-line-reset">';
		ele += '<div class="m-r-2x"><img class="aty-thumb" src="' + list.cover + '" alt=""></div>';
		ele += '<div class="weui-media-box__bd">';
		ele += '<h4 class="weui-media-box__title">' + list.title + '</h4>';
		ele += '<p class="weui-media-box__desc">' + list.content + '</p>';
		ele += '</div>';
		ele += '</div>';
		ele += '<div class="weui-panel__ft">';
		ele += '<p class="fs14r c-888 p-x-2x p-y-1x clearfix">';
		ele += '<i class="icon-to_view m-r-1x fs18r" style="vertical-align: middle;"></i>' + list.reading;
		ele += '<font class="float-right">' + tool.formatDate('yy/mm/dd', list.create_time) + '</font>';
		ele += '</p>';
		ele += '</div>';
		ele += '</a>';
	}
	$('#advisory-template').html(ele);
}

// 读取单个资讯详情回调
function __complete_imdetail(res) {
	$.hideLoading();
	if(res.status == 200) {
		data_imdetail[res.data.id] = res.data;
		bindimdetail(res.data);
	} else {
		// nothing
	}
}

// 绑定数据
function bindimdetail(data) {
	checkLocationTitle(data.title);
	$('#bind-de-cover').attr('src', data.cover);
	$('#bind-de-title').html(data.title);
	$('#bind-de-reading').html(data.reading);
	$('#bind-de-create_time').html(tool.formatDate('yy/mm/dd', data.create_time));
	$('#bind-de-content').html('');
	var val = data.content;
	val = val.replace(/&lt;/gi, '<');
	testEditormdView = editormd.markdownToHTML("bind-de-content", {
		markdown: val,
		htmlDecode: "style,script,iframe", // you can filter tags decode
		tocm: true, // Using [TOCM]
		emoji: true,
		taskList: true,
		tex: true, // 默认不解析
		flowChart: true, // 默认不解析
		sequenceDiagram: true, // 默认不解析
	});
}

// 路由
function checkLocationCallback(hash) {
	if('advisory' == hash) {
		if(!jude_im) {
			$.showLoading();
			factory.request('_information_volist');
			jude_im = true;
		}
	}

	if('details' == hash) {
		if(!data_imdetail[imid]) {
			$.showLoading();
			__getdata_imdetail['id'] = imid;
			factory.request('_information_get');
		} else {

		}
	}
}